import serial  # 导入串口相关的库
from time import sleep

def get_bmp_data():
    filepath = 'python/bad_apple_data.txt'
    f = open(filepath)
    bmp_data = []
    for line in f:
        val = line.strip().split(',')
        if len(val) == 0:
            continue
        bmp_data.append([int(x) for x in val])
    f.close()
    return bmp_data

def main():
    com = serial.Serial('com14', 345600, timeout=10) # 设置端口号，波特率，超时时间
    if not com.isOpen():  # 判断端口是否打开成功
        raise "端口打开失败"

    bmp_data = get_bmp_data() # 读取刚刚生成的TXT文件
    for frame in bmp_data:
        ret = com.write(bytes(frame)) # 将数据转换成二进制后发送
        sleep(0.02)


main()
